package com.ruoyi.mall.ware.service.impl;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.mall.ware.feign.ProductFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.ware.mapper.WareSkuMapper;
import com.ruoyi.mall.ware.domain.WareSku;
import com.ruoyi.mall.ware.service.IWareSkuService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商品库存Service业务层处理
 *
 * @author zhangqishu
 * @date 2022-06-14
 */
@Service
public class WareSkuServiceImpl implements IWareSkuService {
    @Autowired
    private WareSkuMapper wareSkuMapper;

    @Autowired
    private ProductFeignService productFeignService;

    /**
     * 查询商品库存
     *
     * @param id 商品库存主键
     * @return 商品库存
     */
    @Override
    public WareSku selectWareSkuById(Long id) {
        return wareSkuMapper.selectWareSkuById(id);
    }

    /**
     * 查询商品库存列表
     *
     * @param wareSku 商品库存
     * @return 商品库存
     */
    @Override
    public List<WareSku> selectWareSkuList(WareSku wareSku) {
        QueryWrapper<WareSku> wrapper = new QueryWrapper<>();
        Long skuId = wareSku.getSkuId();
        if (!("0".equals(String.valueOf(skuId)) || "null".equals(String.valueOf(skuId)))) {
            wrapper.eq("sku_id", skuId);
        }
        Long wareId = wareSku.getWareId();
        if (!("0".equals(String.valueOf(wareId)) || "null".equals(String.valueOf(wareId)))) {
            wrapper.eq("ware_id", wareId);
        }
        return wareSkuMapper.selectList(wrapper);
    }

    /**
     * 新增商品库存
     *
     * @param wareSku 商品库存
     * @return 结果
     */
    @Override
    public int insertWareSku(WareSku wareSku) {
        return wareSkuMapper.insertWareSku(wareSku);
    }

    /**
     * 修改商品库存
     *
     * @param wareSku 商品库存
     * @return 结果
     */
    @Override
    public int updateWareSku(WareSku wareSku) {
        return wareSkuMapper.updateWareSku(wareSku);
    }

    /**
     * 批量删除商品库存
     *
     * @param ids 需要删除的商品库存主键
     * @return 结果
     */
    @Override
    public int deleteWareSkuByIds(Long[] ids) {
        return wareSkuMapper.deleteWareSkuByIds(ids);
    }

    /**
     * 删除商品库存信息
     *
     * @param id 商品库存主键
     * @return 结果
     */
    @Override
    public int deleteWareSkuById(Long id) {
        return wareSkuMapper.deleteWareSkuById(id);
    }

    /**
     * 商品入库
     *
     * @param skuId
     * @param wareId
     * @param skuNum
     */
    @Override
    public void addStock(Long skuId, Long wareId, Long skuNum) {
        //1.判断如果还没有如果库存记录则是新增 否则修改
        List<WareSku> wareSkuList = wareSkuMapper.selectList(new QueryWrapper<WareSku>().eq("sku_id", skuId).eq("ware_id", wareId));
        if (StringUtils.isEmpty(wareSkuList)) {
            WareSku wareSku = new WareSku();
            wareSku.setSkuId(skuId);
            wareSku.setWareId(wareId);
            wareSku.setStock(skuNum);
            //TODO 远程查询sku的名字  如果失败，整个事物不需要回滚
            //1. 自己catch异常
            //2. TODO 还可以用什么办法让异常出现以后不回滚?
            try {
                R info = productFeignService.getInfo(skuId);
                Map<String, Object> data = (Map<String, Object>) info.getData();
                if (info.getCode() == 200) {
                    wareSku.setSkuName((String) data.get("skuName"));
                }

            } catch (Exception e) {

            }

            wareSkuMapper.insert(wareSku);

        } else {
            wareSkuMapper.addStock(skuId, wareId, skuNum);
        }
    }

    @Override
    public boolean saveBatch(Collection<WareSku> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdateBatch(Collection<WareSku> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean updateBatchById(Collection<WareSku> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdate(WareSku entity) {
        return false;
    }

    @Override
    public WareSku getOne(Wrapper<WareSku> queryWrapper, boolean throwEx) {
        return null;
    }

    @Override
    public Map<String, Object> getMap(Wrapper<WareSku> queryWrapper) {
        return null;
    }

    @Override
    public <V> V getObj(Wrapper<WareSku> queryWrapper, Function<? super Object, V> mapper) {
        return null;
    }

    @Override
    public BaseMapper<WareSku> getBaseMapper() {
        return null;
    }
}
